module ParseExpression
where
import TITypes
import Char (isAlpha,isSpace)
import List ((\\), nub, sort)


parse :: String -> (Expr,String)
parse "" = error "parse was somehow given an empty string"
parse ('\\':cs) = (Abs x e,rest)
    where 
      (x,cs')   = (span isAlpha . dropWhile isSpace) cs
      (e,rest)  = (parse . tail . dropWhile (/='.')) cs'
parse ('(':cs)  = (App e1 e2,rest)
    where
      (e1,cs')  = parse cs
      (e2,cs'') = parse cs'
      (_:rest)  = dropWhile (/=')') cs''
parse (c:cs)
    | isSpace c = parse cs
    | isAlpha c = (Var (c:bs),rest)
    | otherwise = error ("parse confused by: " ++ (c:cs))
    where (bs,rest) = span isAlpha cs

parseExp :: String -> Expr
parseExp = fst . parse
